﻿using System;
using System.Web.Mvc;
using Xenta.Attributes;
using Xenta.Utils;

namespace Xenta.Web.Controllers
{
    [Authenticate, InRole("ADMIN")]
    public sealed class RatingController : ExtController
    {
        [HttpPost, PersistState]
        public ActionResult Create(string operation,
            string returnUrl,
            int holderID,
            int accountID,
            int commentID,
            byte value,
            string[] flags)
        {
            try
            {
                Svc.ProcessOperation(operation, new
                {
                    HolderID = holderID,
                    AccountID = accountID,
                    CommentID = commentID,
                    Value = value,
                    Flags = flags != null 
                        ? flags.Merge(",")
                        : null
                });
            }
            catch(Exception ex)
            {
                ModelState.AddModelError("API", ex);
            }
            return Redirect(returnUrl);
        }

        [HttpPost, PersistState]
        public ActionResult Update(string operation,
            string returnUrl,
            int id,
            byte value,
            string[] flags)
        {
            try
            {
                Svc.ProcessOperation(operation, new
                {
                    EntityID = id,
                    Value = value,
                    Flags = flags != null
                        ? flags.Merge(",")
                        : null
                });
            }
            catch(Exception ex)
            {
                ModelState.AddModelError("API", ex);
            }
            return Redirect(returnUrl);
        }

        [HttpGet, PersistState]
        public ActionResult Delete(string operation, string returnUrl, int id)
        {
            try
            {
                Svc.ProcessOperation(operation, new
                {
                    EntityID = id
                });
            }
            catch(Exception ex)
            {
                ModelState.AddModelError("API", ex);
            }
            return Redirect(returnUrl);
        }
    }
}